package com.jjlin.chapter_2;
import java.util.*;

public class CD109{
    public static class Node{
        public int val;
        public Node next;
        public Node(int data){
            this.val = data;
        }
    }

    //环形链表的约瑟夫问题,普通解法的时间复杂度为O(N*M)
    public static int josephusKill(Node head, Node last, int m){
        int cnt = 0;
        while(last != head){
            if(++cnt == m){
                last.next = head.next;
                cnt = 0;
            }else{
                last = last.next;
            }
            head = head.next;
        }
        return head.val;
    }

    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int m = sc.nextInt();
        Node head = new Node(1);
        Node last = head;
        for(int i = 2; i <= n; i++){//建立约瑟夫环
            Node cur = new Node(i);
            last.next = cur;
            last = cur;
        }
        last.next = head;
        System.out.println(josephusKill(head, last, m));
    }
}


